<!DOCTYPE html>
<html>
  <head><meta name="generator" content="Hexo 3.8.0">
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport">
  <meta name="description" content="fengzhaoyang&#39;s blog">
  <meta name="keyword" content="hexo-theme, vuejs">
  
  <link rel="shortcut icon" href="/css/images/logo.png">
  
  <title>
    
    docker自动化shell脚本 | fzy-blog
    
  </title>
  <link href="//cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
  <link href="//cdnjs.cloudflare.com/ajax/libs/nprogress/0.2.0/nprogress.min.css" rel="stylesheet">
  <link href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/tomorrow.min.css" rel="stylesheet">
  <link rel="stylesheet" href="/css/style.css">
  
  <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <script src="//cdnjs.cloudflare.com/ajax/libs/geopattern/1.2.3/js/geopattern.min.js"></script>
  <script src="//cdnjs.cloudflare.com/ajax/libs/nprogress/0.2.0/nprogress.min.js"></script>
  
  <script src="/js/qrious.js"></script>
  
  
  
  
    <!-- MathJax support START -->
    <script type="text/x-mathjax-config">
      MathJax.Hub.Config({
        tex2jax: {
          inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
          processEscapes: true,
          skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
        }
      });
    </script>

    <script type="text/x-mathjax-config">
      MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax(), i;
        for (i=0; i < all.length; i += 1) {
          all[i].SourceElement().parentNode.className += ' has-jax';
        }
      });
    </script>
    <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
    <!-- MathJax support END -->
  


</head>
<div class="wechat-share">
  <img src="/css/images/logo.png">
</div>
  <body>
    <header class="header fixed-header">
  <div class="header-container">
    <a class="home-link" href="/">
      <div class="logo"></div>
      <span>fzy-blog</span>
    </a>
    <ul class="right-list">
      
        <li class="list-item">
          
            <a href="/" class="item-link">Home</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/tags/" class="item-link">Tags</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/archives/" class="item-link">Archives</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/project/" class="item-link">Projects</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/about/" class="item-link">About</a>
          
        </li>
      
    </ul>
    <div class="menu">
      <span class="icon-bar"></span>
      <span class="icon-bar"></span>
      <span class="icon-bar"></span>
    </div>
    <div class="menu-mask">
      <ul class="menu-list">
        
          <li class="menu-item">
            
              <a href="/" class="menu-link">Home</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/tags/" class="menu-link">Tags</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/archives/" class="menu-link">Archives</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/project/" class="menu-link">Projects</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/about/" class="menu-link">About</a>
            
          </li>
        
      </ul>
    </div>
  </div>
</header>

    <div id="article-banner">
  <h2>docker自动化shell脚本</h2>
  <p class="post-date">2019-05-24</p>
  <div class="arrow-down">
    <a href="javascript:;"></a>
  </div>
</div>
<main class="app-body flex-box">
  <!-- Article START -->
  <article class="post-article">
    <section class="markdown-content"><h3 id="docker自动化shell脚本"><a href="#docker自动化shell脚本" class="headerlink" title="docker自动化shell脚本"></a>docker自动化shell脚本</h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#</span>创建startup.sh文件</span><br><span class="line">touch startup.sh</span><br><span class="line"><span class="meta">#</span>编辑文件</span><br><span class="line">vim startup.sh</span><br><span class="line"><span class="meta">#</span>复制下面内容到startup.sh</span><br><span class="line"><span class="meta">#</span>!/bin/bash</span><br><span class="line">appName=demo1</span><br><span class="line">docker stop $appName</span><br><span class="line">sleep 10s</span><br><span class="line">cd /root/docker</span><br><span class="line">docker build -t $appName .</span><br><span class="line">docker run --name=$appName -p 8080:8080 -t $appName</span><br><span class="line"><span class="meta">#</span>实时查看docker启动日志</span><br><span class="line">docker logs -f --tail 10 $appName &amp;</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span>按ESC wq 回车保存文件</span><br><span class="line"><span class="meta">#</span>设置文件可执行权限</span><br><span class="line">chmod 755 startup.sh</span><br></pre></td></tr></table></figure>
<h3 id="docker-常用命令"><a href="#docker-常用命令" class="headerlink" title="docker 常用命令"></a>docker 常用命令</h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#</span>查看docker版本</span><br><span class="line">docker version</span><br><span class="line"><span class="meta">#</span>显示docker系统的信息</span><br><span class="line">docker info</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span>检索image</span><br><span class="line">docker search image_name</span><br><span class="line"><span class="meta">#</span>下载image</span><br><span class="line">docker pull image_name</span><br><span class="line"><span class="meta">#</span>列出镜像列表</span><br><span class="line"><span class="meta">#</span> -a, --all=false Show all images; --no-trunc=false Don't truncate output; -q, --quiet=false Only show numeric IDs</span><br><span class="line">docker images</span><br><span class="line"><span class="meta">#</span>删除一个或者多个镜像</span><br><span class="line"><span class="meta">#</span>-f, --force=false Force; --no-prune=false Do not delete untagged parents</span><br><span class="line">docker rmi image_name</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span>显示一个镜像的历史</span><br><span class="line"><span class="meta">#</span>--no-trunc=false Don't truncate output; -q, --quiet=false Only show numeric IDs</span><br><span class="line">docker history image_name</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span>启动命令</span><br><span class="line">docker run --name springboot-demo1 -p 8080:8080 -t springboot-demo</span><br><span class="line">docker run image_name echo "hello word"</span><br><span class="line">docker run -d -v /data0/bop-fms-ad/logs:/data/logs --name bop-fms-ad --net="host"  --ulimit nofile=200000 -v /etc/localtime:/etc/localtime:ro -m 1536M --memory-swap 2048M -e JAVA_OPTS="-server -Xmx1024m" bop-fms-ad-backup</span><br><span class="line"><span class="meta">#</span>交互式进入容器中</span><br><span class="line">docker run -i -t image_name /bin/bash</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span>在容器中安装新的程序</span><br><span class="line">docker run image_name yum install -y app_name</span><br><span class="line"><span class="meta">#</span>Note：在执行 yum命令的时候，要带上-y参数。如果不指定-y参数的话，yum命令会进入交互模式，需要用户输入命令来进行确认，但在docker环境中是无法响应这种交互的。yum 命令执行完毕之后，容器就会停止，但对容器的改动不会丢失。</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span>列出当前所有正在运行的container</span><br><span class="line">docker ps</span><br><span class="line"><span class="meta">#</span>列出所有的container</span><br><span class="line">docker ps -a</span><br><span class="line"><span class="meta">#</span>列出最近一次启动的container或查询最后一次创建的容器</span><br><span class="line">docker ps -l</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span>保存对容器的修改 本地备份</span><br><span class="line"><span class="meta">#</span>-a, --author="" Author; -m, --message="" Commit message</span><br><span class="line">docker commit ID new_image_name</span><br><span class="line">docker commit $&#123;name&#125; $&#123;name&#125;-backup</span><br><span class="line"><span class="meta">#</span>Note：image相当于类，container相当于实例，不过可以动态给实例安装新软件，然后把这个container用commit命令固化成一个image。</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span>删除所有容器</span><br><span class="line">docker rm `docker ps -a -q`</span><br><span class="line"><span class="meta">#</span>删除单个容器</span><br><span class="line"><span class="meta">#</span>-f, --force=false; -l, --link=false Remove the specified link and not the underlying container; -v, --volumes=false Remove the volumes associated to the container</span><br><span class="line">docker rm Name/ID</span><br><span class="line"><span class="meta">#</span>停止、启动、杀死一个容器</span><br><span class="line">docker stop Name/ID</span><br><span class="line">docker start Name/ID</span><br><span class="line">docker kill Name/ID</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span>列出一个容器里面被改变的文件或者目录，list列表会显示出三种事件，A 增加的，D 删除的，C 被改变的</span><br><span class="line">docker diff Name/ID</span><br><span class="line"><span class="meta">#</span>显示一个运行的容器里面的进程信息</span><br><span class="line"></span><br><span class="line">docker top Name/ID</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span>从容器里面拷贝文件/目录到本地一个路径</span><br><span class="line">docker cp Name:/container_path to_path</span><br><span class="line">docker cp ID:/container_path to_path</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span>重启一个正在运行的容器 </span><br><span class="line"><span class="meta">#</span>-t, --time=10 Number of seconds to try to stop for before killing the container, Default=10</span><br><span class="line">docker restart Name/ID</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span>附加到一个运行的容器上面</span><br><span class="line"><span class="meta">#</span>--no-stdin=false Do not attach stdin; --sig-proxy=true Proxify all received signal to the process</span><br><span class="line">docker attach ID</span><br><span class="line"><span class="meta">#</span>Note：attach命令允许你查看或者影响一个运行的容器。你可以在同一时间attach同一个容器。你也可以从一个容器中脱离出来，是从CTRL-C。</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span>登陆registry server</span><br><span class="line"><span class="meta">#</span>-e, --email="" Email; -p, --password="" Password; -u, --username="" Username</span><br><span class="line">docker login</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span>根据Dockerfile 构建出一个容器</span><br><span class="line"><span class="meta">#</span>build</span><br><span class="line"><span class="meta">#</span> --no-cache=false Do not use cache when building the image</span><br><span class="line"><span class="meta">#</span> -q, --quiet=false Suppress the verbose output generated by the containers</span><br><span class="line"><span class="meta">#</span> --rm=true Remove intermediate containers after a successful build</span><br><span class="line"><span class="meta">#</span> -t, --tag="" Repository name (and optionally a tag) to be applied to the resulting image in case of success</span><br><span class="line"></span><br><span class="line">docker build -t image_name Dockerfile_path</span><br><span class="line">docker push registry.aifzy.com/bop-k8s/$&#123;name&#125;:release</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span>从一个容器中取日志</span><br><span class="line"><span class="meta">#</span>-f, --follow=false Follow log output; -t, --timestamps=false Show timestamps</span><br><span class="line">docker logs Name/ID</span><br><span class="line"><span class="meta">#</span>实时查看运行的容器的日志</span><br><span class="line"><span class="meta">#</span>例：实时查看docker容器名为s12的最后100行日志</span><br><span class="line">sudo docker logs -f --tail 100 s12</span><br><span class="line">sudo docker logs -f -t --tail 10 s12</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span>查看linux 内存</span><br><span class="line">free -m </span><br><span class="line"><span class="meta">#</span>查看磁盘剩余空间</span><br><span class="line">df -hl </span><br><span class="line"><span class="meta">#</span>docker 查看停止的容器的日志</span><br><span class="line">docker ps -a</span><br><span class="line">然后 docker inspect 对应的容器id </span><br><span class="line">找到 LogPath</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span>进入docker环境</span><br><span class="line">docker exec -it enrichupgrade /bin/bash</span><br><span class="line">docker exec -it 6d9ae9e18df5 /bin/sh</span><br></pre></td></tr></table></figure>
<h3 id="Docker中容器的备份、恢复和迁移"><a href="#Docker中容器的备份、恢复和迁移" class="headerlink" title="Docker中容器的备份、恢复和迁移"></a>Docker中容器的备份、恢复和迁移</h3><p><a href="https://www.cnblogs.com/wq3435/p/6516024.html" target="_blank" rel="noopener">https://www.cnblogs.com/wq3435/p/6516024.html</a></p>
</section>
    <!-- Tags START -->
    
    <div class="tags">
      <span>Tags:</span>
      
  <a href="/tags#CI&amp;CD">
    <span class="tag-code">CI&amp;CD</span>
  </a>

    </div>
    
    <!-- Tags END -->
    <!-- NAV START -->
    
  <div class="nav-container">
    <!-- reverse left and right to put prev and next in a more logic postition -->
    
      <a class="nav-left" href="/2019/05/24/CI&CD/jenkins构建完成后执行shell脚本/">
        <span class="nav-arrow">← </span>
        
          jenkins构建完成后执行shell脚本
        
      </a>
    
    
      <a class="nav-right" href="/2019/05/24/CI&CD/jenkins-pipeline-script部署实例和回滚实例/">
        
          部署操作 jenkins job 任务 pipeline script 脚本
        
        <span class="nav-arrow"> →</span>
      </a>
    
  </div>

    <!-- NAV END -->
    <!-- 打赏 START -->
    
    <div class="money-like">
      <div class="reward-btn">
        赏
        <span class="money-code">
          <span class="alipay-code">
            <div class="code-image"></div>
            <b>使用支付宝打赏</b>
          </span>
          <span class="wechat-code">
            <div class="code-image"></div>
            <b>使用微信打赏</b>
          </span>
        </span>
      </div>
      <p class="notice">若你觉得我的文章对你有帮助，欢迎点击上方按钮对我打赏</p>
    </div>
    
    <!-- 打赏 END -->
    <!-- 二维码 START -->
    
    <div class="qrcode">
      <canvas id="share-qrcode"></canvas>
      <p class="notice">扫描二维码，分享此文章</p>
    </div>
    
    <!-- 二维码 END -->
    
    <!-- No Comment -->
    
  </article>
  <!-- Article END -->
  <!-- Catalog START -->
  
  <aside class="catalog-container">
  <div class="toc-main">
    <strong class="toc-title">Catalog</strong>
    
      <ol class="toc-nav"><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#docker自动化shell脚本"><span class="toc-nav-text">docker自动化shell脚本</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#docker-常用命令"><span class="toc-nav-text">docker 常用命令</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#Docker中容器的备份、恢复和迁移"><span class="toc-nav-text">Docker中容器的备份、恢复和迁移</span></a></li></ol>
    
  </div>
</aside>
  
  <!-- Catalog END -->
</main>

<script>
  (function () {
    var url = 'https://fengzhaoy.github.io/2019/05/24/CI&amp;CD/docker常用命令/';
    var banner = ''
    if (banner !== '' && banner !== 'undefined' && banner !== 'null') {
      $('#article-banner').css({
        'background-image': 'url(' + banner + ')'
      })
    } else {
      $('#article-banner').geopattern(url)
    }
    $('.header').removeClass('fixed-header')

    // error image
    $(".markdown-content img").on('error', function () {
      $(this).attr('src', 'http://file.muyutech.com/error-img.png')
      $(this).css({
        'cursor': 'default'
      })
    })

    // zoom image
    $(".markdown-content img").on('click', function () {
      var src = $(this).attr('src')
      if (src !== 'http://file.muyutech.com/error-img.png') {
        var imageW = $(this).width()
        var imageH = $(this).height()

        var zoom = ($(window).width() * 0.95 / imageW).toFixed(2)
        zoom = zoom < 1 ? 1 : zoom
        zoom = zoom > 2 ? 2 : zoom
        var transY = (($(window).height() - imageH) / 2).toFixed(2)

        $('body').append('<div class="image-view-wrap"><div class="image-view-inner"><img src="' + src +
          '" /></div></div>')
        $('.image-view-wrap').addClass('wrap-active')
        $('.image-view-wrap img').css({
          'width': `${imageW}`,
          'transform': `translate3d(0, ${transY}px, 0) scale3d(${zoom}, ${zoom}, 1)`
        })
        $('html').css('overflow', 'hidden')

        $('.image-view-wrap').on('click', function () {
          $(this).remove()
          $('html').attr('style', '')
        })
      }
    })
  })();
</script>


<script>
  var qr = new QRious({
    element: document.getElementById('share-qrcode'),
    value: document.location.href
  });
</script>





    <div class="scroll-top">
  <span class="arrow-icon"></span>
</div>
    <footer class="app-footer">
  <p class="copyright">
    &copy; 2019
  </p>
</footer>

<script>
  function async (u, c) {
    var d = document,
      t = 'script',
      o = d.createElement(t),
      s = d.getElementsByTagName(t)[0];
    o.src = u;
    if (c) {
      o.addEventListener('load', function (e) {
        c(null, e);
      }, false);
    }
    s.parentNode.insertBefore(o, s);
  }
</script>
<script>
  async ("//cdnjs.cloudflare.com/ajax/libs/fastclick/1.0.6/fastclick.min.js", function () {
    FastClick.attach(document.body);
  })
</script>

<script>
  var hasLine = 'true';
  async ("//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/highlight.min.js", function () {
    $('figure pre').each(function (i, block) {
      var figure = $(this).parents('figure');
      if (hasLine === 'false') {
        figure.find('.gutter').hide();
      }
      var lang = figure.attr('class').split(' ')[1] || 'code';
      var codeHtml = $(this).html();
      var codeTag = document.createElement('code');
      codeTag.className = lang;
      codeTag.innerHTML = codeHtml;
      $(this).attr('class', '').empty().html(codeTag);
      figure.attr('data-lang', lang.toUpperCase());
      hljs.highlightBlock(block);
    });
  })
</script>
<!-- Baidu Tongji -->

<script src="/js/script.js"></script>
  </body>
</html>